﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WXEntity
{
    class ServiceCodeCache
    {
        public static Dictionary<string, string> ServiceCodeCacheInstance;
    }
    public class ServerEntity
    {
        private object lockObj=new object();
        private string _serviceCode;
        public string ServiceCode
        {
            get { return _serviceCode; }
            set { _serviceCode = value; }
        }

        private string _assembly;
        public string Assembly
        {
            get { return _assembly; }
            set { _assembly = value; }
        }

        private string _nameSpace;
        public string NameSpace
        {
            get { return _nameSpace; }
            set { _nameSpace = value; }
        }

        private string _nameSpaceClassName;
        public string NameSpaceClassName
        {
            get { return _nameSpaceClassName; }
            set { _nameSpaceClassName = value; }
        }

        private string _method;
        public string Method
        {
            get { return _method; }
            set { _method = value; }
        }
        private string _errorMsg;
        public string ErrorMsg
        {
            get { return _errorMsg; }
            set { _errorMsg = value; }
        }

        public ServerEntity(string serviceCode)
        {
            try
            {
                if (serviceCode.Length == 8)
                {
                    string fileCode = serviceCode.Substring(0, 6);
                    string methodCode = serviceCode.Substring(6, 2);
                    XmlDocument configDocument = new XmlDocument();
                    if (ServiceCodeCache.ServiceCodeCacheInstance == null)
                        ServiceCodeCache.ServiceCodeCacheInstance = new Dictionary<string, string>();
                    // 加载配置文件
                    if (ServiceCodeCache.ServiceCodeCacheInstance.ContainsKey(serviceCode))
                    {
                        configDocument.LoadXml(ServiceCodeCache.ServiceCodeCacheInstance[serviceCode]);
                    }
                    else
                    {
                        lock (lockObj)
                        {
                            configDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"ServiceCode\" + fileCode + ".config");
                            ServiceCodeCache.ServiceCodeCacheInstance.Add(serviceCode, configDocument.InnerXml);
                        }
                    }
                    // 获取字段信息
                    XmlNode configNode = configDocument.SelectSingleNode("/config");
                    _assembly = configNode.Attributes["dll"].Value;
                    string type = configNode.Attributes["type"].Value;
                    _nameSpaceClassName = type.Split(',')[0];
                    _nameSpace = type.Split(',')[1];

                    XmlNode ServiceCodeNode = configDocument.SelectSingleNode("//ServiceCode[text()='" + methodCode + "']");
                    _method = ServiceCodeNode.Attributes["Method"].Value;
                    _serviceCode = serviceCode;

                }
                else
                {
                    ErrorMsg = "服务码错误：服务码必须为6位数。(" + serviceCode + ")";
                }
            }
            catch (Exception ex)
            {
                ErrorMsg = "服务码错误：【"+ serviceCode +"】\r\n"+ ex.Message;
            }
        }
    }
}
